package be.kuleuven.cs.mop.gui;


import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.logging.Logger;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.WindowConstants;

import be.kuleuven.cs.mop.app.controllers.Controllers;
import be.kuleuven.cs.mop.domain.model.Task;
import be.kuleuven.cs.mop.gui.dialogs.AcceptDeclineHelperInvitationDialog;
import be.kuleuven.cs.mop.gui.dialogs.AdjustClockValueDialog;
import be.kuleuven.cs.mop.gui.dialogs.CreateProjectDialog;
import be.kuleuven.cs.mop.gui.dialogs.CreateResourceDialog;
import be.kuleuven.cs.mop.gui.dialogs.CreateTaskDialog;
import be.kuleuven.cs.mop.gui.dialogs.CreateUserDialog;
import be.kuleuven.cs.mop.gui.dialogs.FocusWorkDialog;
import be.kuleuven.cs.mop.gui.dialogs.LogInDialog;
import be.kuleuven.cs.mop.gui.dialogs.MakeResourceReservationForTaskDialog;
import be.kuleuven.cs.mop.gui.dialogs.ManageHelperInvitationsDialog;
import be.kuleuven.cs.mop.gui.dialogs.ModifyTaskDialog;
import be.kuleuven.cs.mop.gui.dialogs.QuestionDialog;
import be.kuleuven.cs.mop.gui.dialogs.RemoveProjectDialog;
import be.kuleuven.cs.mop.gui.dialogs.RemoveTaskDialog;
import be.kuleuven.cs.mop.gui.dialogs.UpdateTaskStatusDialog;
import be.kuleuven.cs.mop.gui.logging.LogList;
import be.kuleuven.cs.mop.gui.logging.LogUtils;



/**
 * Represents the user interface
 */
public class TaskManagerGUI extends JFrame
		implements ActionListener {
	private static final long serialVersionUID = 1L;
	
	/**
	 * The <code>Logger</code> instance, used to send message to the logging services
	 */
	private static final Logger LOG = LogUtils.getLogger(TaskManagerGUI.class);
	
	/**
	 * The <code>TaskManager</code> instance, used to access <code>Controller</code>s
	 */
	private final Controllers controllers;
	
	
	public TaskManagerGUI(final Controllers controllers, final LogList log) {
		super("TaskManager");
//		getContentPane().setLayout(new BorderLayout());
		setJMenuBar(new MenuBar(this));
		
		if (controllers == null)
			throw new NullPointerException("TaskManager == NULL");
		this.controllers = controllers;
		
		final JScrollPane scroll = new JScrollPane(log,
				ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scroll.setPreferredSize(new Dimension(600,600));
		add(scroll);
		
		pack();
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setLocationRelativeTo(null);
		setResizable(true);
		setVisible(true);
		
		LOG.info("Welcome to TaskManager");
	}
	
	
	
	@Override
	public void actionPerformed(final ActionEvent event) {
		final String cmd = event.getActionCommand();
		if ((cmd == null) || (cmd.isEmpty())) {
			// ignore
		} else if (cmd.equalsIgnoreCase("AcceptDeclineHelperInvitation")) {
			doAcceptDeclineHelperInvitation();
		} else if (cmd.equalsIgnoreCase("AdjustClockValue")) {
			doAdjustClockValue();
		} else if (cmd.equalsIgnoreCase("CreateProject")) {
			doCreateProject();
		} else if (cmd.equalsIgnoreCase("CreateResource")) {
			doCreateResource();
		} else if (cmd.equalsIgnoreCase("CreateTask")) {
			doCreateTask();
		} else if (cmd.equalsIgnoreCase("CreateUser")) {
			doCreateUser();
		} else if (cmd.equalsIgnoreCase("FocusWork")) {
			doFocusWork();
		} else if (cmd.equalsIgnoreCase("LogIn")) {
			doLogIn();
		} else if (cmd.equalsIgnoreCase("MakeResourceReservationForTask")) {
			doMakeResourceReservationForTask();
		} else if (cmd.equalsIgnoreCase("ManageHelperInvitations")) {
			doManageHelperInvitations();
		} else if (cmd.equalsIgnoreCase("ModifyTaskDetails")) {
			doModifyTaskDetails();
		} else if (cmd.equalsIgnoreCase("RemoveProject")) {
			doRemoveProject();
		} else if (cmd.equalsIgnoreCase("RemoveTask")) {
			doRemoveTask();
		} else if (cmd.equalsIgnoreCase("UpdateTaskStatus")) {
			doUpdateTaskStatus();
		}
	}
	
	public boolean askYesNo(final String question) {
		return (JOptionPane.showConfirmDialog(
				this, question, "Please confirm", JOptionPane.YES_NO_OPTION)
				== JOptionPane.YES_OPTION);
	}
	
	public boolean askYesNo(final String question, final Collection<?> data) {
		return new QuestionDialog(this, question, data).getAnswer();
	}
	
	/**
	 * Performs the use case "Adjust Clock Value"
	 */
	protected void doAdjustClockValue() {
		new AdjustClockValueDialog(this, getControllers());
	}
	
	/**
	 * Performs the use case "Accept/Decline Helper Invitation"
	 */
	protected void doAcceptDeclineHelperInvitation() {
		new AcceptDeclineHelperInvitationDialog(this, getControllers());
	}
	
	/**
	 * Performs the use case "Create Project"
	 */
	protected void doCreateProject() {
		new CreateProjectDialog(this, getControllers());
	}
	
	/**
	 * Performs the use case "Create Resource"
	 */
	protected void doCreateResource() {
		new CreateResourceDialog(this, getControllers());
	}
	
	/**
	 * Performs the use case "Create Task"
	 */
	protected void doCreateTask() {
		new CreateTaskDialog(this, getControllers());
	}
	
	/**
	 * Performs the use case "Create User"
	 */
	protected void doCreateUser() {
		new CreateUserDialog(this, getControllers());
	}
	
	/**
	 * Performs the use case "Focus Work"
	 */
	protected void doFocusWork() {
		new FocusWorkDialog(this, getControllers());
	}
	
	/**
	 * Performs the use case "Log In"
	 */
	protected void doLogIn() {
		new LogInDialog(this, getControllers());
	}
	
	/**
	 * Performs the use case "Make Resource Reservation"
	 */
	protected void doMakeResourceReservationForTask() {
		new MakeResourceReservationForTaskDialog(this, getControllers());
	}
	
	/**
	 * Performs the use case "Manage Helper Invitations"
	 */
	protected void doManageHelperInvitations() {
		new ManageHelperInvitationsDialog(this, getControllers());
	}
	
	/**
	 * Performs the use case "Modify Task Details"
	 */
	protected void doModifyTaskDetails() {
		new ModifyTaskDialog(this, getControllers(), null);
	}
	
	/**
	 * Performs the use case "Modify Task Details" for the specified {@link Task}
	 * 
	 * @param task - the {@link Task} on which to perform the use case
	 */
	protected void doModifyTaskDetails(final Task task) {
		if (task != null)
			new ModifyTaskDialog(this, getControllers(), task);
	}
	
	/**
	 * Performs the use case "Remove Project"
	 */
	protected void doRemoveProject() {
		new RemoveProjectDialog(this, getControllers());
	}
	
	/**
	 * Performs the use case "Remove Task"
	 */
	protected void doRemoveTask() {
		new RemoveTaskDialog(this, getControllers());
	}
	
	/**
	 * Performs the use case "Update Task Status"
	 */
	protected void doUpdateTaskStatus() {
		new UpdateTaskStatusDialog(this, getControllers(), null);
	}
	
	/**
	 * Performs the use case "Update Task Status" for the specified {@link Task}
	 * 
	 * @param task - the {@link Task} on which to perform the use case
	 */
	protected void doUpdateTaskStatus(final Task task) {
		if (task != null)
			new UpdateTaskStatusDialog(this, getControllers(), task);
	}
	
	private final Controllers getControllers() { return controllers; }
	
}